import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/localization/localization.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/utils/log.dart';

class LocalLanguageDropDown extends StatelessWidget {
  final UserController _usercontroller = Get.find();
  final box = Hive.box('appconfig');
  @override
  Widget build(BuildContext context) {
    // var profile =
    //     json.decode(box.get(CacheManagerKey['profilesettings'].toString()));
    // bool interfaceChinese = profile["interfaceChinese"];
    // var  localeLangs;
    // if (interfaceChinese) {
    //   localeLangs = LocalizationService.localeLangs;
    // } else {
    //   localeLangs = LocalizationService.localeChineseLangs;
    // }
    return Obx(() => DropdownButton(
          icon: Icon(
            Icons.language,
            color: Theme.of(context).primaryColorLight,
          ),
          value: _usercontroller.selectedLang.value,
          items: LocalizationService.localeLangs.map((String lang) {
            return DropdownMenuItem(
                value: lang,
                child: Text(
                  lang,
                  style: Theme.of(context).textTheme.titleSmall,
                  overflow: TextOverflow.ellipsis,
                ));
          }).toList(),
          onChanged: (String? value) {
            try {
              _usercontroller.setMyLang(value as String);
              switch (value) {
                case 'ENG':
                  _usercontroller.profile.value.lang = 0;
                  break;
                case '简体':
                  _usercontroller.profile.value.lang = 1;
                  break;
                case '繁体':
                  _usercontroller.profile.value.lang = 2;
                  break;
                // case '한국인':
                //   _usercontroller.profile.value.lang = 3;
                //   break;
                default:
                  _usercontroller.profile.value.lang = 0;
              }
              LocalizationService().changeLocale(value);
            } catch (error) {
              LocalizationService().changeLocale('ENG');
              Log.debug(error);
            }
          },
        ));
  }
}
